﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.Approval;

namespace CardLogistics.DAL.Approval
{
    /// <summary>
    /// 审批表_数据访问类
    /// </summary>
    public class ApprovalInfoData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "ApprovalInfo", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        #region 通过内容ID获取主键ID
        /// <summary>
        /// 根据内容ID查询基本信息
        /// </summary>
        /// <param name="InfoId">内容ID</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoByInfoId(string InfoId)
        {
            return GetList(" InfoId = '" + InfoId + "' ", "*");
        }
        #endregion

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ApprovalInfo GetModel(string rId)
        {
            ApprovalInfo model = new ApprovalInfo();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["USERID"] != null)
                {
                    model.USERID = row["USERID"].ToString();
                }
                if (row["APPSTATUS"] != null && row["APPSTATUS"].ToString() != "")
                {
                    model.APPSTATUS = decimal.Parse(row["APPSTATUS"].ToString());
                }
                if (row["APPCONTENT"] != null)
                {
                    model.APPCONTENT = row["APPCONTENT"].ToString();
                }
                if (row["INFOTYPE"] != null && row["INFOTYPE"].ToString() != "")
                {
                    model.INFOTYPE = decimal.Parse(row["INFOTYPE"].ToString());
                }
                if (row["INFOID"] != null)
                {
                    model.INFOID = row["INFOID"].ToString();
                }
                if (row["ADDDATE"] != null && row["ADDDATE"].ToString() != "")
                {
                    model.ADDDATE = DateTime.Parse(row["ADDDATE"].ToString());
                }
                if (row["APPDATE"] != null && row["APPDATE"].ToString() != "")
                {
                    model.APPDATE = DateTime.Parse(row["APPDATE"].ToString());
                }
                if (row["ADDUSER"] != null)
                {
                    model.ADDUSER = row["ADDUSER"].ToString();
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(ApprovalInfo model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
					new OracleParameter("vUSERID", OracleType.VarChar,50),
					new OracleParameter("vAPPSTATUS", OracleType.Number,4),
					new OracleParameter("vAPPCONTENT", OracleType.VarChar,500),
					new OracleParameter("vINFOTYPE", OracleType.Number,4),
					new OracleParameter("vINFOID", OracleType.VarChar,50),
					new OracleParameter("vADDDATE", OracleType.DateTime),
					new OracleParameter("vAPPDATE", OracleType.DateTime),
					new OracleParameter("vADDUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEDATE", OracleType.DateTime)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.USERID;
                parameters[2].Value = model.APPSTATUS;
                parameters[3].Value = model.APPCONTENT;
                parameters[4].Value = model.INFOTYPE;
                parameters[5].Value = model.INFOID;
                parameters[6].Value = model.ADDDATE;
                parameters[7].Value = model.APPDATE;
                parameters[8].Value = model.ADDUSER;
                parameters[9].Value = model.LASTUPDATEUSER;
                parameters[10].Value = model.LASTUPDATEDATE;
                string StrOutPar = DbHelperSQL.RunProcedure("P_ApprovalInfo_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(ApprovalInfo model)
        {
            try
            {
                     OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,50),
					new OracleParameter("vUSERID", OracleType.VarChar,50),
					new OracleParameter("vAPPSTATUS", OracleType.Number,4),
					new OracleParameter("vAPPCONTENT", OracleType.VarChar,500),
					new OracleParameter("vINFOTYPE", OracleType.Number,4),
					new OracleParameter("vINFOID", OracleType.VarChar,50),
					new OracleParameter("vADDDATE", OracleType.DateTime),
					new OracleParameter("vAPPDATE", OracleType.DateTime),
					new OracleParameter("vADDUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEDATE", OracleType.DateTime),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                     parameters[0].Value = model.ID;
                     parameters[1].Value = model.USERID;
                     parameters[2].Value = model.APPSTATUS;
                     parameters[3].Value = model.APPCONTENT;
                     parameters[4].Value = model.INFOTYPE;
                     parameters[5].Value = model.INFOID;
                     parameters[6].Value = model.ADDDATE;
                     parameters[7].Value = model.APPDATE;
                     parameters[8].Value = model.ADDUSER;
                     parameters[9].Value = model.LASTUPDATEUSER;
                     parameters[10].Value = model.LASTUPDATEDATE;
                     parameters[11].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_ApprovalInfo_Update", parameters, parameters[11]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_ApprovalInfo_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
